﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HumanManagement.BusinessLogic;
using HumanManagement.BusinessObjects;
using HumanManagement.DataAccess;
using System.Data;

namespace BSC_HumanManagement.Usercontrols
{
    public partial class uc_QuaTrinhBoNhiem_Detail : System.Web.UI.UserControl
    {
        int itemID = 0;
        LeaderPositionAppointment objLeaderPositionAppointment = new LeaderPositionAppointment();
        LeaderPositionAppointmentBL objLeaderPositionAppointmentBL = new LeaderPositionAppointmentBL();

        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtEffectiveDate.MaxDate = DateTime.MaxValue;
            this.txtEffectiveDate.MinDate = DateTime.MinValue;
            this.txtExpireDate.MaxDate = DateTime.MaxValue;
            this.txtExpireDate.MinDate = DateTime.MinValue;
            //this.txtExpireDate.SelectedDate = DateTime.MaxValue;
            PersonalProfileBL _objPersonalProfileBL = new PersonalProfileBL();
            foreach (DataRow item in _objPersonalProfileBL.GetDataSet(Convert.ToInt32(Session["PersonalProfileID"].ToString())).Tables[0].Rows)
            {
                this.ddlPersonalProfileID.Items.Add(new ListItem(item["FullName"].ToString(), item["ID"].ToString()));
            }
            DepartmentLeaderPositionBL objDepartmentLeaderPositionBL = new DepartmentLeaderPositionBL();
            foreach (DataRow item in objDepartmentLeaderPositionBL.GetDataSet().Tables[0].Rows)
            {
                this.ddlDepartmentLeaderPosition.Items.Add(new ListItem(item["Description"].ToString(), item["ID"].ToString()));
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Quá trình bổ nhiệm Detail")
            {
                LoadData();
                DisableControl();
                ShowData();
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Quá trình bổ nhiệm Edit")
            {
                if (LoadData())
                {
                    ShowData();
                    this.btnDelete.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hồ sơ nhân viên mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Quá trình bổ nhiệm Delete")
            {
                if (LoadData())
                {
                    ShowData();
                    this.btnSave.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hợp đồng mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
        }

        private void ShowData()
        {
            for (int i = 0; i < this.ddlPersonalProfileID.Items.Count; i++)
            {
                if (this.ddlPersonalProfileID.Items[i].Value.Equals(objLeaderPositionAppointment.PersonalProfileID.ToString()))
                {
                    this.ddlPersonalProfileID.SelectedIndex = i;
                }
            }
            for (int i = 0; i < this.ddlDepartmentLeaderPosition.Items.Count; i++)
            {
                if (this.ddlDepartmentLeaderPosition.Items[i].Value.Equals(objLeaderPositionAppointment.DepartmentLeaderPositionID.ToString()))
                {
                    this.ddlDepartmentLeaderPosition.SelectedIndex = i;
                }
            }
            this.txtEffectiveDate.SelectedDate = objLeaderPositionAppointment.EffectiveDate;
            this.txtExpireDate.SelectedDate = objLeaderPositionAppointment.ExpireDate;
            for (int i = 0; i < this.ddlManagementStatus.Items.Count; i++)
            {
                if (this.ddlManagementStatus.Items[i].Value == objLeaderPositionAppointment.AppointmentStatus)
                {
                    this.ddlManagementStatus.SelectedIndex = i;
                }
            }
        }

        private void DisableControl()
        {
            this.ddlManagementStatus.Enabled = false;
            this.ddlPersonalProfileID.Enabled = false;
            this.ddlDepartmentLeaderPosition.Enabled = false;
            this.txtEffectiveDate.Enabled = false;
            this.txtExpireDate.Enabled = false;
        }

        private bool LoadData()
        {
            itemID = Convert.ToInt32(Request.QueryString["ID"]);
            this.objLeaderPositionAppointment = this.objLeaderPositionAppointmentBL.GetByID(itemID, Convert.ToInt32(Session["PersonalProfileID"].ToString()));
            if (this.objLeaderPositionAppointment == null)
            {
                return false;
            }
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.objLeaderPositionAppointment.PersonalProfileID = Convert.ToInt32(this.ddlPersonalProfileID.SelectedValue);
            this.objLeaderPositionAppointment.DepartmentLeaderPositionID = Convert.ToInt32(this.ddlDepartmentLeaderPosition.SelectedValue);
            this.objLeaderPositionAppointment.EffectiveDate = (DateTime)this.txtEffectiveDate.SelectedDate;
            this.objLeaderPositionAppointment.ExpireDate = (DateTime)this.txtExpireDate.SelectedDate;
            this.objLeaderPositionAppointment.AppointmentStatus = this.ddlManagementStatus.SelectedValue;
            this.objLeaderPositionAppointment.LastDate = DateTime.Now;

            if (Request.QueryString["ID"] == null)
            {
                objLeaderPositionAppointmentBL.Add(objLeaderPositionAppointment);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Tạo mới thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Quá trình bổ nhiệm");
            }

            if (Request.QueryString["FunctionName"] == "Quá trình bổ nhiệm Edit")
            {
                objLeaderPositionAppointmentBL.Update(objLeaderPositionAppointment);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Sửa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Quá trình bổ nhiệm");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                //objJobPositionManagement = objJobPositionManagementBL.GetByID(Convert.ToInt32(Request.QueryString["ID"]), Convert.ToInt32(Session["PersonalProfileID"].ToString()));
                //objJobPositionManagement.ManagementStatus = "0";
                //objJobPositionManagementBL.Update(objJobPositionManagement);
                //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Xóa thành công')", true);
                //Response.Redirect("/Default.aspx?FunctionName=Quá trình bổ nhiệm");
            }
            else { }
        }
    }
}